<?php
/*
 * @Description: file content
 * @FilePath: \auxface_certificate\app\certificate\controller\admin\v1\machineRisk\SubForm.php
 */

declare(strict_types=1);

namespace app\certificate\controller\admin\v1\machineRisk;

use think\facade\Db;
use think\Request;

class SubForm
{
    /**
     * @Description: 
     * @param {Request} $request
     */
    public function getList(Request $request)
    {
        $data  = $request->data;
        $limit =  $data['limit'];
        $page =  $data['page'];

        $where = [
            ['sf.projectCode', '=', $request->prjCode],
            ['sf.isDelete', '=', 0],
        ];

        if (!empty($data['name'])) array_push($where, ['sf.name', 'like', $data['name'] . "%"]);

        $list = DB::table('certificate_subform')->alias('sf')
            ->leftJoin('certificate_flow f','f.uuid=sf.flowUuid')
            ->where($where)
            ->field(['sf.name', 'sf.remark', 'sf.inDate', 'sf.uuid', 'sf.flowUuid', 'f.name as flowName', 'sf.subtitle'])
            ->order('sf.id desc')
            ->page((int)$page, (int)$limit)
            ->select();

        $count = DB::table('certificate_subform')->alias('sf')
            ->leftJoin('certificate_flow f','f.uuid=sf.flowUuid')
            ->where($where)
            ->count();

        $data = compact('list', 'count');
        return result(SUCCESS, $data);
    }
    /**
     * @Description: 
     * @param {Request} $request
     */
    public function create(Request $request)
    {
        $data = $request->data;

        $insert =  [
            'uuid' => createUuid(),
            'projectCode' => $request->prjCode,
            'corpCode' => $request->corpCode,
            'name' => $data['name'],
            'remark' => $data['remark'] ?? '',
            'flowUuid' => $data['flowUuid'] ?? '',
            'subtitle' => $data['subtitle'] ?? '',
        ];
        DB::table('certificate_subform')->insert($insert);

        return result(SUCCESS, ['uuid' => $insert['uuid'], 'inDate' => date('Y-m-d H:i:s')], '添加成功!');
    }

    /**
     * @Description: 修改
     */
    public function update(Request $request)
    {
        $data = $request->data;
        $uuid = $data['uuid'];

        $cehck = DB::table('certificate_subform')->where([
            ['uuid', '<>', $uuid],
            ['name', '=', $data['name']],
            ['isDelete', '=', 0],
        ])->value('id');
        if ($cehck) return result(FAIL, '', '名称已存在,无法修改!');
        $updateData = [
            'name' => $data['name'],
            'remark' => $data['remark'] ?? '',
            'flowUuid' => $data['flowUuid'] ?? '',
            'subtitle' => $data['subtitle'] ?? '',
        ];
        DB::table('certificate_subform')->where('uuid', $uuid)->update($updateData);
        return result(SUCCESS, '', '更改成功');
    }

    public function destroy(Request $request)
    {
        $data = $request->data;
        DB::table('certificate_subform')->where('uuid', $data['uuid'])->update(['isDelete' => 1]);

        //删除类型关联的附表
        $machineRiskTypeList = DB::table('certificate_machineRiskType')->where('subFormUuids', 'like', '%' . $data['uuid'] . '%')->select()->toArray();
        foreach ($machineRiskTypeList as $item) {
            $fileTypeUuids = json_decode($item['subFormUuids'], true) ?: [];
            $fileTypeUuids = array_diff($fileTypeUuids, [$data['uuid']]) ?: [];
            DB::table('certificate_machineRiskType')->where('uuid', $item['uuid'])->update(['subFormUuids' => json_encode($fileTypeUuids, JSON_UNESCAPED_UNICODE)]);
        }
        return result(SUCCESS, '', '删除成功');
    }

     /**
     * @Description: 获取数据
     * @param {Request} $request
     */
    public function getDetail(Request $request)
    {
        $data = $request->data;
        $uuid = $data['uuid'];

        $data = DB::table('certificate_subform')->where('uuid', $uuid)->field(['inDate', 'corpCode', 'name', 'remark', 'uuid','flowUuid','subtitle'])->find();

        return result(SUCCESS, $data, '');
    }

     /**
     * @Description: 获取字典
     * @param {Request} $request
     */
    public function getOptions(Request $request)
    {
        $where = [
            ['projectCode', '=', $request->prjCode],
            ['isDelete', '=', 0]
        ];

        $data['flowList'] = DB::table('certificate_flow')
            ->where($where)
            ->field(['name', 'remark', 'uuid'])
            ->order('id desc')
            ->select();

        return result(SUCCESS, $data, '');
    }
}
